var baseUrl = require('../../utils/urllink.js')
//获取应用实例
var app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,


    showTopTips: false,
    showTopTipsText: '',
    show:false,
    textarea_show:true,

    mobile:'',
    verfiyCode: '',
    invitCode: '',

    textarea_counter:'',
    c2:'',
    currentTime:180,
    vcode:'vcode',

  },
  



  onReady:function(){
    console.log("onready");
  },

  onLoad: function (options) {
    console.log("onLoad");
    // if (app.globalData.userInfo) {
      
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true
    //   })
    // } else if (this.data.canIUse) {
      
    //   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userInfoReadyCallback = res => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // } else {
      
    //   // 在没有 open-type=getUserInfo 版本的兼容处理
    //   wx.getUserInfo({
    //     success: res => {
    //       app.globalData.userInfo = res.userInfo
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //     }
    //   })
    // }

    //登录调用
    getApp().getUserInfo(function (userInfo, openid1) {

    })

    //调用应用实例的方法获取全局数据
    var that = this
    var invitCode = options.invitCode
    if(invitCode != null){
      that.setData({
        invitCode: invitCode,
        textarea_show: false,
      })
    }
    that.setData({
        userInfo: app.globalData.userInfo,
        show: true,
    })


    
  },

  onShow:function(){
    console.log("onShow");
  },
  getUserInfo: function (e) {
    console.log(e)
    //app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  //获取验证码
  vcode: function (e) {
    var that = this
    if (this.data.mobile.length <= 0) {
      that.setData({
        showTopTips: true,
        showTopTipsText: '请输入手机号码',
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
      if (!myreg.test(this.data.mobile)) {
        that.setData({
          showTopTips: true,
          showTopTipsText: '请输入正确的手机号码',
        });
        setTimeout(function () {
          that.setData({
            showTopTips: false
          });
        }, 3000);
      }else{
        // if (this.c2 && this.c2.interval) return !1
        // this.c2 = new $wuxCountDown({
        //   date: +(new Date) + 60000,
        //   onEnd() {
        //     this.setData({
        //       c2: '重新获取验证码',
        //     })
        //   },
        //   render(date) {
        //     const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
        //     date.sec !== 0 && this.setData({
        //       c2: sec,
        //     })
        //   },
        // })

        var total_micro_second = 60;    //表示60秒倒计时，想要变长就把60修改更大
        //验证码倒计时
        count_down(that, total_micro_second);
        //调用 获取验证码方法
        getcode(that.data.mobile)
      }



    }

  },
  // 获取推广码
  getincitcode:function(e){
    var that = this;
    that.setData({
      textarea_show: false,
    })
  },
  //获取input的数据
  bindmobile:function(e){

    this.setData({
     mobile: e.detail.value
    })

  },
  //获取input的数据
  bindverfiyCode: function (e) {
    console.log(e.detail.value);
    this.setData({
      verfiyCode:e.detail.value
    })
  },
  //获取input的数据
  bindinvitCode: function (e) {
    console.log(e.detail.value);
    this.setData({
      invitCode: e.detail.value
    })
  },
  //获取input的数据
  bindtextarea_counter:function(e){
    console.log(e.detail.value);
    this.setData({
      textarea_counter: e.detail.value
    })
  },

  //成为推广者B
  tuiguangzhe:function(e){
    var that = this
    //调用方法 注册 成为B
    register(that, that.data.mobile, that.data.verfiyCode, that.data.invitCode, that.data.textarea_counter, app.globalData.user_id, 1)
    
  },

  //成为a 并且跳转申请A开店
  kaidian: function (e) {
    var that = this
    //调用方法 注册 成为A
    register(that, that.data.mobile, that.data.verfiyCode, that.data.invitCode, that.data.textarea_counter,app.globalData.user_id,2)
  },


})


//获取验证码
function getcode(mobile) {
  //请求服务器
  wx.request({
    url: baseUrl +'index.php?m=Home&c=Api&a=send_validate_code&scene=6&',
   
    data: {
      mobile: mobile,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//注册成为A或者B
function register(that, mobile, verfiyCode, invitCode, textarea_counter,user_id,agent_type){
  console.log(mobile)
  //判断空值
  if(mobile.length<=0){
    console.log("手机号为空")
    that.setData({
      showTopTips: true,
      showTopTipsText: '手机号为空',
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  } else if (verfiyCode.length <= 0){
    console.log("验证码为空")
    that.setData({
      showTopTips: true,
      showTopTipsText: '验证码为空',
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
    
  } else if (agent_type == 1){
    that.setData({
      textarea_show: false
    });
    if ((invitCode == '' || invitCode == undefined) && (textarea_counter == '' || textarea_counter == undefined)) {

      that.setData({
        showTopTips: true,
        showTopTipsText: '推广码为空',
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      //请求服务器 提交成为B
      wx.request({
        url: baseUrl +'weapp/LoginApi/userReg.html',
        data: {
          mobile: mobile,
          verfiyCode: verfiyCode,
          invitCode: invitCode,
          textarea_counter: textarea_counter,
          user_id: user_id,
          agent_type: agent_type,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          if (res.data.code == 0) {
            //判断跳转 agent_type=2 申请开店页面
            if (agent_type == 2) {
              wx.navigateTo({
                url: '../kaidian/kaidian',
              })
            }
            if (agent_type == 1) {
              wx.switchTab({
                url: '../mine/mine',
              })
            }
          } else if (res.data.code == -1) {
            console.log("操作失败")
            that.setData({
              showTopTips: true,
              showTopTipsText: '邀请码输入错误',
            });
            setTimeout(function () {
              that.setData({
                showTopTips: false
              });
            }, 3000);
          }else if(res.data.code == -2){
            that.setData({
              showTopTips: true,
              showTopTipsText: '短信验证码错误',
            });
            setTimeout(function () {
              that.setData({
                showTopTips: false
              });
            }, 3000);
          }else if (res.data.code == -5) {
            that.setData({
              showTopTips: true,
              showTopTipsText: '手机号码已被使用',
            });
            setTimeout(function () {
              that.setData({
                showTopTips: false
              });
            }, 3000);
          }

        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })
    }

  } else {

    wx.navigateTo({
      url: '../kaidian/kaidian',
    })
  }

}


/* 毫秒级倒计时 */
function count_down(that, total_micro_second) {
  if (total_micro_second <= 0) {
    that.setData({
      c2: "重新发送",
      vcode:'vcode',

    });
    // timeout则跳出递归
    return;
  }

  // 渲染倒计时时钟
  that.setData({
    c2: total_micro_second + " 秒",
    vcode:'',

  });

  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 1;
    count_down(that, total_micro_second);
  }, 1000)



}